package com.kermel.andromeda.scheduler;

import java.util.Calendar;

import android.os.CountDownTimer;

import com.kermel.andromeda.data.CountdownTimer;
import com.kermel.common.util.DateTimeUtils;

public class CountdownTimerScheduler extends CountDownTimer {
    private CountdownTimer countdownTimer;
    private OnFinishListener onFinishListener;

    public CountdownTimerScheduler(CountdownTimer countdownTimer) {
        super(countdownTimer.triggerTime, 1000);
        
        this.countdownTimer = countdownTimer;
        
        System.out.println(String.format(">>>     right now: %s", DateTimeUtils.rightNowToDateStringFull()));
        debugTriggerTime();
    }
    
    private void debugTriggerTime() {
        Calendar now = Calendar.getInstance();
        
        now.add(Calendar.HOUR_OF_DAY, countdownTimer.hoursFromNow);
        now.add(Calendar.MINUTE, countdownTimer.minutesFromNow);
        
        long countdownTimerTriggerTime = now.getTimeInMillis();
        System.out.println(String.format(">>> Timer trigger: %s", DateTimeUtils.timeInMillisecondsToDateStringFull(countdownTimerTriggerTime)));
    }
    
    @Override
    public void onFinish() {
        notifyListenerCountdownTimerIsFinished();
        //launchCountdownTimerActivity();
    }
    
    private void notifyListenerCountdownTimerIsFinished() {
        if (onFinishListener != null)
            onFinishListener.onFinish();
    }
    
    @Override
    public void onTick(long millisUntilFinished) {
        System.out.println(String.format(">>> onTick() - %s", DateTimeUtils.rightNowToDateStringFull()));
    }
    
    public static interface OnFinishListener {
        public void onFinish();
    }

    public OnFinishListener getOnFinishListener() {
        return onFinishListener;
    }

    public void setOnFinishListener(OnFinishListener onFinishListener) {
        this.onFinishListener = onFinishListener;
    }
}
